package com.br.android.widing;

import java.text.DecimalFormat;
import java.util.Currency;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.br.android.widing.db.WidingGanhosDAO;
import com.br.android.widing.entity.Ganho;

public class ExibicaoGanhoActivity extends SherlockActivity {

	private DecimalFormat format;
	
	private Ganho ganho;
	
	private TextView textViewNome;
	private TextView textViewCategoria;
	private TextView textViewData;
	private TextView textViewValor;
	
	private WidingGanhosDAO dao;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_exibicao_ganho);
		
		dao = new WidingGanhosDAO(this);
		ganho = new Ganho();
		
		Currency currency = Currency.getInstance(Locale.getDefault());
		format = new DecimalFormat(currency.getSymbol() + " ###,###,##0.00");
		
		textViewNome = (TextView) findViewById(R.id.textViewExibirNomeGanho);
		textViewCategoria = (TextView) findViewById(R.id.textViewExibirCategoriaGanho);
		textViewData = (TextView) findViewById(R.id.textViewExibirDataGanho);
		textViewValor = (TextView) findViewById(R.id.textViewExibirValorGanho);
		
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayoutExibicaoGanho);
		
		 BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_img);
         bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
         layout.setBackgroundDrawable(bg);
         
         Intent it = getIntent();
         ganho.setIdGanho(it.getExtras().getInt("id"));
         ganho.setNomeGanho(it.getExtras().getString("nomeganho"));
         ganho.setCategoriaGanho(it.getExtras().getString("categoriaganho"));
         ganho.setDataGanho(it.getExtras().getString("dataganho"));
         ganho.setValorGanho(it.getExtras().getDouble("valorganho"));
         
         textViewNome.setText(ganho.getNomeGanho());
         textViewCategoria.setText(ganho.getCategoriaGanho());
         textViewData.setText(ganho.getDataGanho());
         textViewValor.setText(format.format(ganho.getValorGanho()));
         
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	@Override
	public void onBackPressed() {
		startActivity(new Intent(this, PrincipalActivity.class));
		overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu_exibicao_ganho, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuEditarGanho:
			Intent intent = new Intent(this, EditarGanhoActivity.class);
			intent.putExtra("ganho", ganho);
			startActivity(intent);
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
			
		case R.id.menuExcluirGanho:
			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle(ganho.getNomeGanho());
			alert.setMessage("tem certeza que deseja excluir este ganho?");
			alert.setPositiveButton("Excluir", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dao.excluirGasto(ganho.getIdGanho());
					dialog.dismiss();
					startActivity(new Intent(ExibicaoGanhoActivity.this, PrincipalActivity.class));
					overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
					finish();
				}
			});
			alert.setNegativeButton("Cancelar", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			alert.show();
			break;
		
		case android.R.id.home:
			startActivity(new Intent(this, PrincipalActivity.class));
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
